#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main()
{
    pid_t pid;
    pid = fork();
    if(pid < 0){//fork失败
    	printf("%s fork error\n",__FUNCTION__);
    	return 1;
    }else if( pid == 0 ){ 
        //child执行
        printf("child is run, pid is : %d\n",getpid());
        sleep(5);
        exit(1);
    } else{
        //father执行
        int status = 0;
        pid_t ret = 0;
        do{
            ret = waitpid(-1, &status, WNOHANG);//非阻塞式等待
            if( ret == 0 ){//等待失败则继续等待
                printf("child is running\n");
            }
            sleep(1);
        }while(ret == 0);
        //等待成功打印对应信息
        if( WIFEXITED(status) && ret == pid ){
            //退出正常输出退出码
            printf("wait child 5s success, child return code is :%d.\n",WEXITSTATUS(status));
        }else{
            //退出异常
            printf("wait child failed, return.\n");
            return 1;
        }
    }
    return 0;
}
